package game

import (
	"qm_system_server/server/global"
	"qm_system_server/server/model/game"
    gameReq "qm_system_server/server/model/game/request"
)

type QmServerService struct {
}

// CreateQmServer 创建qmServer表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmServerService *QmServerService) CreateQmServer(qmServer *game.QmServer) (err error) {
	err = global.GVA_DB.Create(qmServer).Error
	return err
}

// DeleteQmServer 删除qmServer表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmServerService *QmServerService)DeleteQmServer(ID string) (err error) {
	err = global.GVA_DB.Delete(&game.QmServer{},"id = ?",ID).Error
	return err
}

// DeleteQmServerByIds 批量删除qmServer表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmServerService *QmServerService)DeleteQmServerByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]game.QmServer{},"id in ?",IDs).Error
	return err
}

// UpdateQmServer 更新qmServer表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmServerService *QmServerService)UpdateQmServer(qmServer game.QmServer) (err error) {
	err = global.GVA_DB.Save(&qmServer).Error
	return err
}

// GetQmServer 根据ID获取qmServer表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmServerService *QmServerService)GetQmServer(ID string) (qmServer game.QmServer, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&qmServer).Error
	return
}

// GetQmServerInfoList 分页获取qmServer表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmServerService *QmServerService)GetQmServerInfoList(info gameReq.QmServerSearch) (list []game.QmServer, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&game.QmServer{})
    var qmServers []game.QmServer
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&qmServers).Error
	return  qmServers, total, err
}
